﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.Infastructure;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public sealed class DataGridChildRow<T> where T : class
    {
        public DataGridChildRow(DataGrid<T> dataGrid, T dataItem, int index)
        {
            if (dataGrid == null)
            {
                throw new ArgumentNullException("dataGrid");
            }

            if (dataItem == null)
            {
                throw new ArgumentNullException("dataItem");
            }

            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            HtmlAttributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            DataGrid = dataGrid;
            DataItem = dataItem;
            Index = index;
            IsAlternate = (index % 2) != 0;
        }

        public DataGrid<T> DataGrid
        {
            get;
            private set;
        }

        public T DataItem
        {
            get;
            private set;
        }

        public int Index
        {
            get;
            private set;
        }

        public bool IsAlternate
        {
            get;
            private set;
        }

        public IDictionary<string, object> HtmlAttributes
        {
            get;
            private set;
        }

        public HtmlTemplate<T> Template
        {
            get;
            set;
        }

        public Func<T, bool> Conditional
        {
            get;
            set;
        }

        public void SetTemplate(Action<T, IViewComponentWriter> template)
        {
            if (Template == null)
            {
                Template = new HtmlTemplate<T>();
            }

            Template.ActionTemplate = template;
        }

        public void SetTemplate(Func<T, object> template)
        {
            if (Template == null)
            {
                Template = new HtmlTemplate<T>();
            }

            Template.InlineTemplate = template;
        }
    }
}
